//
//  Spiel5ViewController.m
//  Lextra Junior ueb IT
//
//  Created by Dirk Lieder on 30.05.11.
//  Copyright 2011 Phosphon. All rights reserved.
//

#import "Spiel5ViewController.h"
#import "AsyncImageView.h"
#import "NavigationViewController.h"

#import "SpielCounter.h"
#import "Lextra_Engine.h"
#import "getFilenameHD.h"

#import <stdlib.h>
#import <time.h>
//___NGUYEN_HUY_SON___
@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"iPad_rightview_bar_background.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end
//___END_NGUYEN_HUY_SON
@implementation Spiel5ViewController
//___NGUYEN_HUY_SON___IPAD_ONLY
@synthesize woerView;
@synthesize navigationBar;
@synthesize label1;
@synthesize buttonNewWord;
//END_NGUYEN_HUY_SON
@synthesize outCountLabel;
@synthesize resetButton;
@synthesize frageLabel;
@synthesize animView;
@synthesize gameButton1,gameButton2,gameButton3,gameButton4;

@synthesize richtigLabel;
@synthesize bildView;
@synthesize frLabel,deLabel;
@synthesize playButtonB1,playButtonB2;

@synthesize view1,view2;

@synthesize spielCounter;


static NSMutableArray *myArray;
static int myArrayPtr;

static NSMutableArray *questArray;
static int questArrayPtr;

static int allQuestions,allTime;

AVAudioPlayer *audioPlayer;

int animationCounter;
int animationPuffer;

bool animationRunning;

- (BOOL)isPadPortrait
{
    
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationPortrait
                || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}


- (BOOL)isPadLandscape
{
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
                || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}

- (void)setDelegatePlud:(id)new_delegate{
	Delegate = new_delegate;
    
    // reset Game
    
    if(myArray) [myArray dealloc];
    //__NGUYEN_HUY_SON
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //Neu iPad
    {
        myArray = [[NSMutableArray alloc] initWithArray:[woerView getGameListPoinds] copyItems:YES]; 
    }
    else
    {
        myArray = [[NSMutableArray alloc] initWithArray:[(Lextra_Engine*)Delegate getGameListPoinds] copyItems:YES]; 
    }
    //END_NGUYEN_HUY_SON
    //myArray = [Delegate getGameListPoinds];
    
    NSMutableArray *tmpArray = [[NSMutableArray alloc] init];

    int i=[myArray count];
    while (i>0) {
        i--;
        int tmpObj = arc4random() % ([myArray count]);
        
        [tmpArray addObject:[myArray objectAtIndex:tmpObj]];
        [myArray removeObjectAtIndex:tmpObj];        
    }
    myArrayPtr = 0;
    myArray = tmpArray;
    
    allQuestions = 0;
    allTime = 0;
    //__NGUYEN__HUY_SON
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        CGFloat navBarHeight = 44.0f;    
        CGRect frame = CGRectMake(navigationBar.frame.origin.x, navigationBar.frame.origin.y, navigationBar.frame.size.width, navBarHeight);
        [navigationBar setFrame:frame];
        
    }  //___END_NGUYEN_HUY_SON

    [self newQuest];
    
   // NSLog(@"++%@", myArray);

}

- (void)newQuest{
    
    self.view = view1;
    
    [self stop:NULL];
    
    if(allQuestions == 0){
        resetButton.hidden = true;
        outCountLabel.hidden = true;
    } else {
        resetButton.hidden = false;
        outCountLabel.hidden = false;
        
        NSString *outText = [NSString stringWithFormat:@"Mittlere Zeit: %i Sekunden",allTime/allQuestions];
        outCountLabel.text = outText;
    }
    
    //if(questArray) [questArray dealloc];
    questArray = [[NSMutableArray alloc] init];
   
    // get 4 new questions
    int i=4;
    while (i>0) {
        i--;
        
        [questArray addObject:[myArray objectAtIndex:myArrayPtr]];
        
        myArrayPtr++;
        if(myArrayPtr >=[myArray count]) myArrayPtr = 0;
    }
    
    questArrayPtr = arc4random() % 4;
    
    // show Label 
    NSString *tmpString1;
    tmpString1 =  [self getWithArtikel:[questArray objectAtIndex:questArrayPtr]];
    frageLabel.text = tmpString1;

    // label buttons
    NSMutableString *tmpString2;
    if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:0] objectForKey:@"origDE"]] length] !=0){
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:0] objectForKey:@"origDE"]];
    } else {
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:0] objectForKey:@"bezeichnungDE"]];
    }
    [gameButton1 setTitle:tmpString2 forState:UIControlStateNormal];
    [gameButton1 setTitle:tmpString2 forState:UIControlStateSelected];
    [gameButton1 setTitle:tmpString2 forState:UIControlStateHighlighted];
   
    if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:1] objectForKey:@"origDE"]] length] !=0){
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:1] objectForKey:@"origDE"]];
    } else {
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:1] objectForKey:@"bezeichnungDE"]];
    }
    [gameButton2 setTitle:tmpString2 forState:UIControlStateNormal];
    [gameButton2 setTitle:tmpString2 forState:UIControlStateSelected];
    [gameButton2 setTitle:tmpString2 forState:UIControlStateHighlighted];
    
    if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:2] objectForKey:@"origDE"]] length] !=0){
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:2] objectForKey:@"origDE"]];
    } else {
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:2] objectForKey:@"bezeichnungDE"]];
    }
    [gameButton3 setTitle:tmpString2 forState:UIControlStateNormal];
    [gameButton3 setTitle:tmpString2 forState:UIControlStateSelected];
    [gameButton3 setTitle:tmpString2 forState:UIControlStateHighlighted];

    if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:3] objectForKey:@"origDE"]] length] !=0){
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:3] objectForKey:@"origDE"]];
    } else {
        tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:3] objectForKey:@"bezeichnungDE"]];
    }
    [gameButton4 setTitle:tmpString2 forState:UIControlStateNormal];
    [gameButton4 setTitle:tmpString2 forState:UIControlStateSelected];
    [gameButton4 setTitle:tmpString2 forState:UIControlStateHighlighted];

    
    // start anim
    animationRunning = true;
    
    NSTimeInterval fps = 0.02;

    timer = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self selector: @selector( loop )];
    [timer setFrameInterval: fps];
    [timer addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];

    
       
    //NSLog(@"++%@", questArray);
}


-(void) loop {
    
    if(animationRunning){
        animationRunning = false;
        
        animationCounter = 0;
        animationPuffer = -1;
        
        [self.spielCounter setTicks:0];
        
        if(self.spielCounter == nil){
            //___NGUYEN_HUY_SON
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
            {
                spielCounter = [[SpielCounter alloc] initWithNibName:@"SpielCounter" bundle:nil];
                if([self isPadPortrait] == TRUE)
                {
                    spielCounter.view.frame = CGRectMake(250, 0, navigationBar.frame.size.width-300, 44);
                }
                else
                {
                    spielCounter.view.frame = CGRectMake(230, 0, navigationBar.frame.size.width-300, 44);
                }
                spielCounter.myCounter.frame = spielCounter.view.frame;
                spielCounter.myCounter.textAlignment = UITextAlignmentCenter;
                [spielCounter setDelegate:self];
            }
            else
            {
                SpielCounter *home = [[SpielCounter alloc] initWithNibName:@"SpielCounter" bundle:nil];
                self.spielCounter = home;
                
                CGRect frame = home.view.frame;
                frame.origin.x += 200.0;
                //___END_NGUYEN_HUY_SON
                home.view.frame = frame;
                
                [home setDelegate:self];
                [home release];
            }
        }
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
        {
            [navigationBar addSubview:self.spielCounter.view];
            
            // show Label 
            NSString *tmpString1;
            tmpString1 =  [self getWithArtikel:[questArray objectAtIndex:questArrayPtr]];
            frageLabel.text = tmpString1;
            
            // label buttons
            NSMutableString *tmpString2 = [[NSMutableString alloc] init];
            if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:0] objectForKey:@"origDE"]] length] !=0){
                tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:0] objectForKey:@"origDE"]];
            } else {
                tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:0] objectForKey:@"bezeichnungDE"]];
            }
            [gameButton1 setTitle:tmpString2 forState:UIControlStateNormal];
            [gameButton1 setTitle:tmpString2 forState:UIControlStateSelected];
            [gameButton1 setTitle:tmpString2 forState:UIControlStateHighlighted];
            // NSMutableString *tmpString3;
            if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:1] objectForKey:@"origDE"]] length] !=0){
                tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:1] objectForKey:@"origDE"]];
            } else {
                tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:1] objectForKey:@"bezeichnungDE"]];
            }
            [gameButton2 setTitle:tmpString2 forState:UIControlStateNormal];
            [gameButton2 setTitle:tmpString2 forState:UIControlStateSelected];
            [gameButton2 setTitle:tmpString2 forState:UIControlStateHighlighted];
            
            if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:2] objectForKey:@"origDE"]] length] !=0){
                tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:2] objectForKey:@"origDE"]];
            } else {
                tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:2] objectForKey:@"bezeichnungDE"]];
            }
            [gameButton3 setTitle:tmpString2 forState:UIControlStateNormal];
            [gameButton3 setTitle:tmpString2 forState:UIControlStateSelected];
            [gameButton3 setTitle:tmpString2 forState:UIControlStateHighlighted];
            
            if([[[NSMutableString alloc] initWithString:[[questArray objectAtIndex:3] objectForKey:@"origDE"]] length] !=0){
                tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:3] objectForKey:@"origDE"]];
            } else {
                tmpString2 = [[NSMutableString alloc] initWithString:[[questArray objectAtIndex:3] objectForKey:@"bezeichnungDE"]];
            }
            [gameButton4 setTitle:tmpString2 forState:UIControlStateNormal];
            [gameButton4 setTitle:tmpString2 forState:UIControlStateSelected];
            [gameButton4 setTitle:tmpString2 forState:UIControlStateHighlighted];
        }
        else
        {
            NavigationViewController *tmp = [Delegate getNavigationID];
            [tmp.view addSubview:self.spielCounter.view];
        }
    }
    
    animationCounter++;
    animationCounter++;
    if(animationCounter > 9999) animationCounter = 9999;
    if(animationCounter > 99) [self.spielCounter setTicks:animationCounter];
	
    
    if(animationPuffer != animationCounter/100){
        animationPuffer = animationCounter/100;
        
        // show bild
        //___NGUYEN_HUY_SON
        NSString *substring;
        NSString *myName = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"bild"];
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //Neu iPad
        {
            substring = [PHOSPHON imagePost:myName imagePostfix:@"_m@2x"];
        }
        else
        {
            substring = [PHOSPHON imagePost:myName imagePostfix:@"_m"];
        }        
        //----END_NGUYEN_HUY_SON
        UIImage *img = [[UIImage imageNamed:substring] retain]; 
        if(animationCounter<9000) img = [self scale:img toSize:CGSizeMake(roundf(animationPuffer), roundf(animationPuffer))];
        [animView setImage:img];
    }
}


- (UIImage *)scale:(UIImage *)image toSize:(CGSize)size 
{ 
    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext 
    (); 
    UIGraphicsEndImageContext(); 
    return scaledImage; 
}

- (IBAction) game1:(id)sender{
    if(questArrayPtr == 0){
        [self showResult:true];
    } else {
        animationCounter += 2000;
        if(animationCounter > 9999) animationCounter = 9999;
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"falsch" ofType:@"m4a"]]; 
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [audioPlayer play];
    }
}
- (IBAction) game2:(id)sender{
    if(questArrayPtr == 1){
        [self showResult:true];
    } else {
        animationCounter += 2000;
        if(animationCounter > 9999) animationCounter = 9999;
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"falsch" ofType:@"m4a"]];
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [audioPlayer play];
    }
}
- (IBAction) game3:(id)sender{
    if(questArrayPtr == 2){
        [self showResult:true];
    } else {
        animationCounter += 2000;
        if(animationCounter > 9999) animationCounter = 9999;
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"falsch" ofType:@"m4a"]]; 
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [audioPlayer play];
    }
}
- (IBAction) game4:(id)sender{
    if(questArrayPtr == 3){
        [self showResult:true];
    } else {
        animationCounter += 2000;
        if(animationCounter > 9999) animationCounter = 9999;
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"falsch" ofType:@"m4a"]]; 
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [audioPlayer play];
    }
}

- (IBAction) naechsteswort:(id)sender{
    [self newQuest];
}

- (IBAction) reset:(id)sender{
    
    [self stop:NULL];

    allQuestions = 0;
    allTime = 0;
    
    [self newQuest];
}


- (void)showResult:(Boolean)result{
    [self stopAndHideCounter];
    
    self.view = view2;
   
    [self stop:NULL]; // stop sound
    
    // auswertung
    allQuestions++;
    allTime += animationCounter/100;

    richtigLabel.hidden = false;
        
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"richtig" ofType:@"m4a"]]; 
    if(audioPlayer) {
        [audioPlayer stop];
        [audioPlayer release];
    }
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [audioPlayer play];

    
      // show bild
    NSString *myName = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"bild"];
    NSString *substring = [PHOSPHON imagePost:myName imagePostfix:@"_m"];
	UIImage * img = [[UIImage imageNamed:substring] retain]; 
	[bildView setImage:img];

    // show text
    NSString *tmpString1;
    tmpString1 =  [self getWithArtikel:[questArray objectAtIndex:questArrayPtr]];
        
    NSMutableString *tmpString2;
    if([[[questArray objectAtIndex:questArrayPtr] objectForKey:@"origDE"] length] !=0){
        tmpString2 = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"origDE"];
    } else {
        tmpString2 = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"bezeichnungDE"];
    }

    frLabel.text = tmpString1;
    deLabel.text = tmpString2;
    NSString *outText;
    if(animationCounter/100 != 1){
        outText = [NSString stringWithFormat:@"Du hast %i Sekunden gebraucht.",animationCounter/100];
    } else {
        outText = [NSString stringWithFormat:@"Du hast %i Sekunde gebraucht.",animationCounter/100];
    }
    richtigLabel.text = outText;
    
}

- (void)stopAndHideCounter{
    
    if(timer) {
        [timer removeFromRunLoop:[NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
        timer = Nil;
    }
    if(self.spielCounter) {
        [self.spielCounter.view removeFromSuperview];
    }
    
}



-(NSString*) getWithArtikel:(NSDictionary*)inDic{

    NSMutableString *tmpString = [[NSMutableString alloc] initWithFormat:@"%@", [inDic objectForKey:@"artikel"]];
    
    if([tmpString length] > 0){
        if(![tmpString hasSuffix:@"'"]){
            [tmpString appendString:@" "];
        }
    }
    
    if([[inDic objectForKey:@"origFR"] length] !=0){
        [tmpString appendString:[inDic objectForKey:@"origFR"]]; 
    } else {
        [tmpString appendString:[inDic objectForKey:@"bezeichnungFR"]]; 
    }

    return tmpString;
}


- (IBAction) play:(id)sender{
	
    // first test
    NSString *myFilename;
    NSString *myName = [[questArray objectAtIndex:questArrayPtr] objectForKey:@"ton"];
    
    if([myName hasSuffix:@".mp3"]){
        myFilename = [myName substringToIndex:[myName length]-4];
    } else {
        myFilename = [[NSString alloc] initWithString:myName];
    }
    
    //NSLog(@"--%@", myFilename);
    
    if(myName){
        NSURL *url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:myFilename ofType:@"m4a"]];
        if(audioPlayer) {
            [audioPlayer stop];
            [audioPlayer release];
        }
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        audioPlayer.delegate = self;
        [audioPlayer play];
        [url release];
    }
    playButtonB1.hidden = true;
    playButtonB2.hidden = false;

}

- (IBAction) stop:(id)sender{
    
    if(audioPlayer) [audioPlayer stop];
    playButtonB1.hidden = false;
    playButtonB2.hidden = true;
   
}

- (void)audioPlayerDidFinishPlaying:IntroPlayer successfully: (BOOL)flag
{
    if(audioPlayer) [audioPlayer stop];
    playButtonB1.hidden = false;
    playButtonB2.hidden = true;

}
- (void)sstop{
    if(audioPlayer) [audioPlayer stop];
    playButtonB1.hidden = false;
    playButtonB2.hidden = true;
    
    [self stopAndHideCounter];
    self.view = view1;
    
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        srandom(time(NULL));
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        [navigationBar setTintColor:[[UIColor alloc] initWithRed:20.0 / 255 green:59.0 / 255 blue:102.0 / 255 alpha:1.0]];
    }
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//__NGUYEN_HUY_SON
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        return YES;
    }
    else
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
}
//___END_NGUYEN_HUY_SON___
//______NGUYEN_HUY_SON___________________________________________________________________________________________
#pragma mark -
#pragma mark Managing the popover
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Add the popover button to the left navigation item.
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
    spielCounter.view.frame = CGRectMake(220,0,468,44);
    spielCounter.myCounter.frame = spielCounter.view.frame;
    spielCounter.myCounter.textAlignment = UITextAlignmentCenter;

    NSLog(@"%f-%f-%f-%f_____%f______",gameButton1.frame.size.width,animView.frame.size.width,frLabel.frame.size.width,deLabel.frame.size.width,view1.frame.size.width);
    CGRect newFrame_lable1 = CGRectMake((768 - frageLabel.frame.size.width - 10)/2 ,120 , frageLabel.frame.size.width, frageLabel.frame.size.height);
    frageLabel.frame = newFrame_lable1;
    
    CGRect newFrame_Image = CGRectMake((768 - animView.frame.size.width)/2 ,160 , animView.frame.size.width, animView.frame.size.height);
    animView.frame = newFrame_Image;
    
    CGRect newFrame_lable11 = CGRectMake((768 - label1.frame.size.width)/2 ,500 , label1.frame.size.width, label1.frame.size.height);
    label1.frame = newFrame_lable11;
    //
    CGRect newFrame_game1 = CGRectMake(768/2 - gameButton1.frame.size.width -5 ,560  , gameButton1.frame.size.width, gameButton1.frame.size.height);
    gameButton1.frame = newFrame_game1;
    //
    CGRect newFrame_game2 = CGRectMake( 768/2 - gameButton1.frame.size.width -5,600 , gameButton2.frame.size.width, gameButton2.frame.size.height);
    gameButton2.frame = newFrame_game2;
    //
    CGRect newFrame_game3 = CGRectMake(768/2,560 , gameButton3.frame.size.width, gameButton3.frame.size.height);
    gameButton3.frame = newFrame_game3;
    //
    CGRect newFrame_game4 = CGRectMake(768/2,600 , gameButton4.frame.size.width, gameButton4.frame.size.height);
    gameButton4.frame = newFrame_game4;
    
    bildView.frame= newFrame_Image;
    buttonNewWord.frame = CGRectMake((768-buttonNewWord.frame.size.width)/2,609 , buttonNewWord.frame.size.width, buttonNewWord.frame.size.height);
    deLabel.frame = CGRectMake((768-deLabel.frame.size.width)/2,577 , deLabel.frame.size.width, deLabel.frame.size.height);
    playButtonB1.frame = CGRectMake((768-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
    playButtonB2.frame = CGRectMake((768-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
    frLabel.frame = CGRectMake((768-frLabel.frame.size.width)/2,132 , frLabel.frame.size.width, frLabel.frame.size.height);
    richtigLabel.frame = CGRectMake((768-richtigLabel.frame.size.width)/2,107 , richtigLabel.frame.size.width, richtigLabel.frame.size.height);

}


- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Remove the popover button.
    [navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
    spielCounter.view.frame = CGRectMake(240, 0, 503, 44);
    spielCounter.myCounter.frame = spielCounter.view.frame;
    spielCounter.myCounter.textAlignment = UITextAlignmentCenter;
    CGRect newFrame_lable1 = CGRectMake((703 - frageLabel.frame.size.width - 10)/2 ,120 , frageLabel.frame.size.width, frageLabel.frame.size.height);
    frageLabel.frame = newFrame_lable1;
    
    CGRect newFrame_Image = CGRectMake((703 - animView.frame.size.width)/2 ,160 , animView.frame.size.width, animView.frame.size.height);
    animView.frame = newFrame_Image;
    
    CGRect newFrame_lable11 = CGRectMake((703 - label1.frame.size.width)/2 ,500 , label1.frame.size.width, label1.frame.size.height);
    label1.frame = newFrame_lable11;
    //
    CGRect newFrame_game1 = CGRectMake(703/2 - gameButton1.frame.size.width -5 ,560  , gameButton1.frame.size.width, gameButton1.frame.size.height);
    gameButton1.frame = newFrame_game1;
    //
    CGRect newFrame_game2 = CGRectMake( 703/2 - gameButton1.frame.size.width -5,600 , gameButton2.frame.size.width, gameButton2.frame.size.height);
    gameButton2.frame = newFrame_game2;
    //
    CGRect newFrame_game3 = CGRectMake(703/2,560 , gameButton3.frame.size.width, gameButton3.frame.size.height);
    gameButton3.frame = newFrame_game3;
    //
    CGRect newFrame_game4 = CGRectMake(703/2,600 , gameButton4.frame.size.width, gameButton4.frame.size.height);
    gameButton4.frame = newFrame_game4;
    
    bildView.frame= newFrame_Image;
    buttonNewWord.frame = CGRectMake((703-buttonNewWord.frame.size.width)/2,609 , buttonNewWord.frame.size.width, buttonNewWord.frame.size.height);
    deLabel.frame = CGRectMake((703-deLabel.frame.size.width)/2,577 , deLabel.frame.size.width, deLabel.frame.size.height);
    playButtonB1.frame = CGRectMake((703-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
    playButtonB2.frame = CGRectMake((703-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
    frLabel.frame = CGRectMake((703-frLabel.frame.size.width)/2,132 , frLabel.frame.size.width, frLabel.frame.size.height);
    richtigLabel.frame = CGRectMake((703-richtigLabel.frame.size.width)/2,107 , richtigLabel.frame.size.width, richtigLabel.frame.size.height);

}


- (void) viewDidAppear:(BOOL)animated
{
    if([self isPadPortrait] == TRUE)
    {

        CGRect newFrame_lable1 = CGRectMake((768 - frageLabel.frame.size.width - 10)/2 ,120 , frageLabel.frame.size.width, frageLabel.frame.size.height);
        frageLabel.frame = newFrame_lable1;
        
        CGRect newFrame_Image = CGRectMake((768 - animView.frame.size.width)/2 ,160 , animView.frame.size.width, animView.frame.size.height);
        animView.frame = newFrame_Image;
        
        CGRect newFrame_lable11 = CGRectMake((768 - label1.frame.size.width)/2 ,500 , label1.frame.size.width, label1.frame.size.height);
        label1.frame = newFrame_lable11;
        //
        CGRect newFrame_game1 = CGRectMake(768/2 - gameButton1.frame.size.width -5 ,560  , gameButton1.frame.size.width, gameButton1.frame.size.height);
        gameButton1.frame = newFrame_game1;
        //
        CGRect newFrame_game2 = CGRectMake( 768/2 - gameButton1.frame.size.width -5,600 , gameButton2.frame.size.width, gameButton2.frame.size.height);
        gameButton2.frame = newFrame_game2;
        //
        CGRect newFrame_game3 = CGRectMake(768/2,560 , gameButton3.frame.size.width, gameButton3.frame.size.height);
        gameButton3.frame = newFrame_game3;
        //
        CGRect newFrame_game4 = CGRectMake(768/2,600 , gameButton4.frame.size.width, gameButton4.frame.size.height);
        gameButton4.frame = newFrame_game4;
        
        bildView.frame= newFrame_Image;
        buttonNewWord.frame = CGRectMake((768-buttonNewWord.frame.size.width)/2,609 , buttonNewWord.frame.size.width, buttonNewWord.frame.size.height);
        deLabel.frame = CGRectMake((768-deLabel.frame.size.width)/2,577 , deLabel.frame.size.width, deLabel.frame.size.height);
        playButtonB1.frame = CGRectMake((768-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
        playButtonB2.frame = CGRectMake((768-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
        frLabel.frame = CGRectMake((768-frLabel.frame.size.width)/2,132 , frLabel.frame.size.width, frLabel.frame.size.height);
        richtigLabel.frame = CGRectMake((768-richtigLabel.frame.size.width)/2,107 , richtigLabel.frame.size.width, richtigLabel.frame.size.height);

    }
    else
    {

        CGRect newFrame_lable1 = CGRectMake((703 - frageLabel.frame.size.width - 10)/2 ,120 , frageLabel.frame.size.width, frageLabel.frame.size.height);

        frageLabel.frame = newFrame_lable1;
        
        CGRect newFrame_Image = CGRectMake((703 - animView.frame.size.width)/2 ,160 , animView.frame.size.width, animView.frame.size.height);
        animView.frame = newFrame_Image;
        
        CGRect newFrame_lable11 = CGRectMake((703 - label1.frame.size.width)/2 ,500 , label1.frame.size.width, label1.frame.size.height);
        label1.frame = newFrame_lable11;
        //
        CGRect newFrame_game1 = CGRectMake(703/2 - gameButton1.frame.size.width -5 ,560  , gameButton1.frame.size.width, gameButton1.frame.size.height);
        gameButton1.frame = newFrame_game1;
        //
        CGRect newFrame_game2 = CGRectMake( 703/2 - gameButton1.frame.size.width -5,600 , gameButton2.frame.size.width, gameButton2.frame.size.height);
        gameButton2.frame = newFrame_game2;
        //
        CGRect newFrame_game3 = CGRectMake(703/2,560 , gameButton3.frame.size.width, gameButton3.frame.size.height);
        gameButton3.frame = newFrame_game3;
        //
        CGRect newFrame_game4 = CGRectMake(703/2,600 , gameButton4.frame.size.width, gameButton4.frame.size.height);
        gameButton4.frame = newFrame_game4;
        
        bildView.frame= newFrame_Image;
        buttonNewWord.frame = CGRectMake((703-buttonNewWord.frame.size.width)/2,609 , buttonNewWord.frame.size.width, buttonNewWord.frame.size.height);
        deLabel.frame = CGRectMake((703-deLabel.frame.size.width)/2,577 , deLabel.frame.size.width, deLabel.frame.size.height);
        playButtonB1.frame = CGRectMake((703-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
        playButtonB2.frame = CGRectMake((703-playButtonB1.frame.size.width)/2,508 , playButtonB1.frame.size.width, playButtonB1.frame.size.height);
        frLabel.frame = CGRectMake((703-frLabel.frame.size.width)/2,132 , frLabel.frame.size.width, frLabel.frame.size.height);
        richtigLabel.frame = CGRectMake((703-richtigLabel.frame.size.width)/2,107 , richtigLabel.frame.size.width, richtigLabel.frame.size.height);

    }
    [super viewDidAppear:animated];
}
@end
